package com.example.testglide;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class Mybaseadp extends BaseAdapter {
    String [] urls;
    Bitmap bmptmp = null;
    public Mybaseadp(String [] urls){
        this.urls = urls;
    }

    @Override
    public int getCount() {
        return urls.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder myvh; //ImageView itemimg;
        if(convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.ly_itemimg, parent, false);
            myvh = new ViewHolder();
            myvh.iv = convertView.findViewById(R.id.iv_itemimg);
            convertView.setTag(myvh);
        }else{
            myvh = (ViewHolder) convertView.getTag();
        }
        Log.i("mytag", "url:"+urls[position]);
        Glide.with(parent.getContext()).load(urls[position])
                .into(myvh.iv);

        /*new Thread(){
            public void run(){
                bmptmp = downloadimg(urls[position]);
            }
        }.start();
        try {
            wait();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        myvh.iv.setImageBitmap(bmptmp);*/
        //this.notifyDataSetChanged();
        return convertView;
    }

    private synchronized Bitmap downloadimg(String url) {
        Bitmap bmp = null;
        HttpURLConnection httpconn = null;
        InputStream is = null;
        if(!"".equals(url)){
            try {
                URL urllink = new URL(url);

                httpconn = (HttpURLConnection) urllink.openConnection();
                httpconn.setDoInput(true);
                Log.i("mytag", "current url:" + url);
                httpconn.connect();
                Log.i("mytag", "http state:" + httpconn.getResponseCode());
                if( httpconn.getResponseCode() == HttpURLConnection.HTTP_OK ){

                    is = httpconn.getInputStream();
                    //BitmapFactory.
                    bmp = BitmapFactory.decodeStream(is);
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally{
                if(is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(httpconn != null){
                    httpconn.disconnect();
                }
                //notify();
            }
        }
        return bmp;
    }

    class ViewHolder {
        ImageView iv;
    }

/*    class Myhandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

        }
    }*/
}
